<?php

class SetImageAction extends CAction
{
    private $_recipe;
    private $_step;
    private $_succ=false;

    private function renderView()
    {
        $this->controller->layout='simple';
        $this->controller->render('setImage',array('recipe'=>$this->_recipe, 'step'=>$this->_step, 'succ'=>$this->_succ));
    }

    private function validateImage()
    {
        if($this->_step->image===null) {
            $this->_step->addError('image','图片不能为空');
            return false;            
        }    
        if(!file_exists($this->_step->image->getTempName())) {
            $this->_step->addError('image','文件大小超过限制');
            return false;            
        }
        if(!UserImage::validateUploadFormat('step', $this->_step->image->getExtensionName())) {
            $this->_step->addError('image','文件格式不正确');
            return false;
        }
        return true;
    }    
        
    public function run()
    {
        if(!isset($_GET['recipeId']) || !isset($_GET['step']))
            throw new CHttpException(400,'the request is invalid');
    
        $this->_step=new Step;
        $this->_step->id=$_GET['step'];
        $this->_recipe=new Recipe;
        $this->_recipe->id=$_GET['recipeId'];
        if(!isset($_POST['Step'])){
            $this->renderView();
            return;        
        }

        $this->_step->image=$_POST['Step']['image'];
        $this->_step->image=CUploadedFile::getInstance($this->_step,'image');
        if(!$this->validateImage()) {
            $this->renderView();
            return;        
        }

        // lazy loading
        $image = $this->_step->image;
        $this->_step=Step::loadObjectById($_GET['step'],array('image'));
        if($this->_step===null)
            throw new CHttpException(404,'The requested step does not exist.');
        if($this->_step->recipeId!=$_GET['recipeId'])
            throw new CHttpException(403,'cannot edit the step not belong to the recipe');
        if($this->_step->userId!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the step not belong to you');

        $this->_recipe=Recipe::loadObjectById($_GET['recipeId'],array('steps'));
        if($this->_recipe===null)
            throw new CHttpException(404,'The requested recipe does not exist.');
        if(array_search($_GET['step'], $this->_recipe->steps)===false)
            throw new CHttpException(404,'The requested step does not belong to the recipe.');
        
        $image=UserImage::create('step', $image->getTempName());
        if($image===false){
            $this->_step->addError('image','保存文件失败');
            $this->renderView();
            return;        
        }
                
        $oldImage=$this->_step->image;
        $this->_step->image=$image;
        if(!Step::saveObject($this->_step, array('image'))) {
            UserImage::delete('step',$this->_step->image);
        }
        else {
            UserImage::delete('step', $oldImage);
            Recipe::saveObject($this->_recipe, array('updateTime'));
            $this->_succ=true;
        }
        
        $this->renderView();
    }
}
  
?>